package com.lsk.servicemap.remote;

import com.lsk.internalcommon.DTO.ResponseResult;
import com.lsk.internalcommon.constant.AMapConfigConstants;
import com.lsk.internalcommon.request.Point;
import com.lsk.internalcommon.request.PointRequestDTO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.RestTemplate;

import java.net.URI;

@Service
public class PointClient {
    @Value("${map.key}")
    private String mapKey;

    @Value("${map.sid}")
    private String sid;

    @Autowired
    private RestTemplate restTemplate;

    public ResponseResult upload(PointRequestDTO pointRequestDTO){
        //拼接url
        StringBuilder url = new StringBuilder();
        url.append(AMapConfigConstants.POINT_ADD_URL);
        url.append("?");
        url.append("key="+mapKey);
        url.append("&");
        url.append("sid="+sid);
        url.append("&");
        url.append("tid="+pointRequestDTO.getTid());
        url.append("&");
        url.append("trid="+pointRequestDTO.getTrid());
        url.append("&");
        url.append("points=");
        Point[] points = pointRequestDTO.getPoints();
        url.append("%5B");
        for (Point p : points
        ) {
            url.append("%7B");
            String locatetime = p.getLocatetime();
            String location = p.getLocation();
            url.append("%22location%22");
            url.append("%3A");
            url.append("%22"+location+"%22");
            url.append("%2C");

            url.append("%22locatetime%22");
            url.append("%3A");
            url.append(locatetime);

            url.append("%7D");
        }
        url.append("%5D");

        ResponseEntity<String> stringResponseEntity = restTemplate.postForEntity(URI.create(url.toString()), null, String.class);
        return ResponseResult.success();
    }
}
